package com.incar.chuxing.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;

import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.incar.chuxing.R;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.DensityUtils;
import com.incar.chuxing.utils.ImageLoader;
import com.incar.chuxing.utils.IntentKey;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;


public class PopPostActivity extends Activity {

    private ImageView ivCancel;
    private ConvenientBanner convenientBanner;
    private List<String> networkImages, clickUrlList, titleList;
    //private float xLocation, yLocation;
    private Context mContext = this;
    private LinearLayout llPoints;
    private Activity activity = this;
/*    private Animation animation = null;
    private FrameLayout relativeLayout;
    private AnimatorSet animSet;
    private AnimatorSet animSet2;*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_post);
        ButterKnife.bind(this);
        this.setFinishOnTouchOutside(false);
        AppManager.getAppManager().addActivity(activity);
        initView();
        initData();
    }

    private void initView() {
        ivCancel = (ImageView) findViewById(R.id.banner_cancel);
        convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner);
        llPoints = (LinearLayout) findViewById(R.id.llPoints);
        // relativeLayout = (FrameLayout) findViewById(R.id.activity_dialog_banner);
    }

    private void initData() {
        Intent intent = getIntent();
        networkImages = intent.getStringArrayListExtra("imageList");
        clickUrlList = intent.getStringArrayListExtra("urlList");
        titleList = intent.getStringArrayListExtra("titleList");
       /* xLocation = DensityUtils.px2dp(mContext, intent.getIntExtra("xLocation", 0));
        yLocation = DensityUtils.px2dp(mContext, intent.getIntExtra("yLocation", 0));*/
/*
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        int top = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            top = getResources().getDimensionPixelSize(resourceId);
        }*/
     /*   float xWidth = DensityUtils.px2dp(mContext, dm.widthPixels);
        float yWidth = DensityUtils.px2dp(mContext, dm.heightPixels - top);
        float x = DensityUtils.dp2px(mContext, xWidth / 2 - (xWidth - xLocation));
        float y = -(DensityUtils.dp2px(mContext, yWidth / 2 - yLocation));
        //进入动画
        ObjectAnimator anim5 = ObjectAnimator.ofFloat(relativeLayout, "scaleX", 0f, 1.0f);
        ObjectAnimator anim6 = ObjectAnimator.ofFloat(relativeLayout, "scaleY", 0f, 1.0f);
        ObjectAnimator anim7 = ObjectAnimator.ofFloat(relativeLayout, "alpha", 0f, 1.0f);
        ObjectAnimator anim8 = ObjectAnimator.ofFloat(relativeLayout, "translationX", x, 0.0f);
        ObjectAnimator anim9 = ObjectAnimator.ofFloat(relativeLayout, "translationY", y, 0.0f);
        animSet2 = new AnimatorSet();
        animSet2.setDuration(800);
        animSet2.setInterpolator(new LinearInterpolator());
        animSet2.playTogether(anim5, anim6, anim7, anim8, anim9);*/
        //初始化指示器
        initDots();
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, networkImages)
                .setPointViewVisible(false)    //设置指示器是否可见
                .stopTurning();//关闭自动切换
        convenientBanner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, titleList.get(position));
                intent.putExtra(IntentKey.INTENT_WEBVIEW_URL, clickUrlList.get(position));
                startActivity(intent);
                finish();
            }
        });
        convenientBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < llPoints.getChildCount(); i++) {
                    llPoints.getChildAt(i).setBackgroundResource(R.drawable.dot_unchoose);
                    if (i == position % networkImages.size()) {
                        llPoints.getChildAt(i).setBackgroundResource(R.drawable.dot_choose);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        // animSet2.start();


      /*  //消失动画
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(relativeLayout, "scaleX", 1.0f, 0f);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(relativeLayout, "scaleY", 1.0f, 0f);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(relativeLayout, "translationX", 0.0f, x);
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(relativeLayout, "translationY", 0.0f, y);
        animSet = new AnimatorSet();
        animSet.setDuration(800);
        animSet.setInterpolator(new LinearInterpolator());
        animSet.playTogether(anim1, anim2, anim3, anim4);

        animSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                EventBus.getDefault().post(new AnimalEvent(true));
                finish();
            }

            @Override
            public void onAnimationCancel(Animator animator) {
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
            }
        });
*/
    }

    private void initDots() {
        for (int i = 0; i < networkImages.size(); i++) {
            View points = new View(mContext);
            points.setBackgroundResource(R.drawable.dot_unchoose);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DensityUtils.dp2px(mContext, 6), DensityUtils.dp2px(mContext, 6));
            params.leftMargin = DensityUtils.dp2px(mContext, 10);
            points.setLayoutParams(params);
            llPoints.addView(points);
        }
        llPoints.getChildAt(0).setBackgroundResource(R.drawable.dot_choose);
    }

    public class NetworkImageHolderView implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            //你可以通过layout文件来创建，也可以像我一样用代码创建，不一定是Image，任何控件都可以进行翻页
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            ImageLoader.getInstance().displayImage(getApplicationContext(), data, imageView);
        }
    }

    @OnClick(R.id.banner_cancel)
    public void onClick() {
        AppManager.getAppManager().finishActivity();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

    @Override
    public void onBackPressed() {
        AppManager.getAppManager().finishActivity();
    }
}
